home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 118_01.zip / RAWFILE.BDS < prev    next >
Text File  |  1993-06-03  |  4KB  |  195 lines

  1. /* Lowest level file routines for Software Tools
  2.  * Used by File primitives
  3.  *
  4.  * source:  rawfile.bds
  5.  * version: July 23, 1981
  6.  */
  7.  
  8. #include tools.h
  9.  
  10. /* make calls to raw BDS file routines more visible */
  11.  
  12. #define BDS_close   _close
  13. #define BDS_creat   _creat
  14. #define BDS_flush   _flush
  15. #define BDS_open    _open
  16. #define BDS_read    _read 
  17. #define BDS_seek    _seek
  18. #define BDS_unlink  _unlink
  19. #define BDS_write   _write
  20.  
  21. /* routines on this file:
  22.  
  23.     _creat
  24.  
  25.     _fcreat        _fclose
  26.     _fflush        _fopen
  27.     _getc        _putc
  28. */
  29.  
  30. /* The following routines are replacements for routines
  31.  * on STDLIB2.CRL
  32.  */
  33.     
  34. BDS_creat (filename)
  35. char *filename;
  36. {
  37.     _unlink (filename);
  38.     /* call BDOS create function */
  39.     if (bdos (22,0x5c) == 255) {
  40.         return(ERR);
  41.     }
  42.     else {
  43.         /* open for writing */
  44.         return(BDS_open(filename,1));
  45.     }
  46. }
  47.  
  48. /* The following routines came from STDLIB1.C
  49.    They all use this stucture:
  50.  
  51. #define NSECTS 8    Number of sectors to buffer in ram
  52.  
  53. note:  (NSECTS * SECSIZ + 6) = sizeof(struct_buf)
  54.  
  55. #define BUFSIZ (NSECTS * SECSIZ + 6 )    Don't touch this
  56.  
  57. struct _buf {                Or this...
  58.     int _fd;            returned by BDS_open
  59.     int _nleft;
  60.     char *_nextp;
  61.     char _buff[NSECTS * SECSIZ];
  62. };
  63.  
  64. */
  65.  
  66.  
  67. int _fopen(filename,iobuf)
  68. struct _buf *iobuf;
  69. char *filename;
  70. {
  71.     
  72.     if ((iobuf -> _fd = BDS_open(filename,0)) < 0) {
  73.         return(ERROR);
  74.     }
  75.     iobuf -> _nleft = 0;
  76.     return iobuf -> _fd;
  77. }
  78.  
  79.  
  80. int _getc (iobuf)
  81. struct _buf *iobuf;
  82. {
  83.     int nsecs;
  84.  
  85.  
  86.     /* comment out -----
  87.     if (iobuf == 0) return getchar();
  88.     if (iobuf == 3) return bdos(3);
  89.     ----- end comment out */
  90.  
  91.     if (iobuf -> _nleft--) {
  92.          return *iobuf -> _nextp++;
  93.     }
  94.  
  95.     nsecs = BDS_read(iobuf -> _fd, iobuf -> _buff, NSECTS);
  96.     if (nsecs <= 0) {
  97.         return iobuf -> _nleft++;
  98.     }
  99.  
  100.     iobuf -> _nleft = nsecs * SECSIZ - 1;
  101.     iobuf -> _nextp = iobuf -> _buff;
  102.     return *iobuf->_nextp++;
  103. }
  104.  
  105.  
  106. int _fcreat(name,iobuf)
  107. char *name;
  108. struct _buf *iobuf;
  109. {
  110.     if ((iobuf -> _fd = BDS_creat(name)) < 0 ) {
  111.         return ERROR;
  112.     }
  113.     iobuf -> _nextp = iobuf -> _buff;
  114.     iobuf -> _nleft = (NSECTS * SECSIZ);
  115.     return iobuf -> _fd;
  116. }
  117.  
  118.  
  119. int _putc (c,iobuf)
  120. char c;
  121. struct _buf *iobuf;
  122. {
  123.  
  124.     int nsecs;
  125.  
  126.  
  127.     /* comment out -----
  128.     if (iobuf == 1) return putchar(c);
  129.     if (iobuf == 2) return (bdos(5,c));
  130.     if (iobuf == 3) return (bdos(4,c));
  131.     ----- end comment out */
  132.  
  133.     if (iobuf -> _nleft--) {
  134.         return *iobuf -> _nextp++ = c;
  135.     }
  136.     nsecs = BDS_write(iobuf->_fd, iobuf->_buff, NSECTS);
  137.     if (nsecs != NSECTS) {
  138.         return ERROR;
  139.     }
  140.     iobuf -> _nleft = (NSECTS * SECSIZ - 1);
  141.     iobuf -> _nextp = iobuf -> _buff;
  142.     return *iobuf -> _nextp++ = c;
  143. }
  144.  
  145.  
  146. int _fflush(iobuf)
  147. struct _buf *iobuf;
  148. {
  149.     int i;
  150.  
  151.     /* comment out -----
  152.     if (iobuf < 4) return OK;
  153.     ----- end comment out */
  154.  
  155.     if (iobuf -> _nleft == (NSECTS * SECSIZ)) {
  156.         return OK;
  157.     }
  158.  
  159.     i = NSECTS - iobuf->_nleft / SECSIZ;
  160.     if (BDS_write(iobuf -> _fd, iobuf -> _buff, i) != i) {
  161.             return ERROR;
  162.     }
  163.  
  164.     i = (i-1) * SECSIZ;
  165.  
  166.     if (iobuf -> _nleft) {
  167.         movmem(iobuf->_buff + i, iobuf->_buff, SECSIZ);
  168.         iobuf -> _nleft += i;
  169.         iobuf -> _nextp -= i;
  170.         return BDS_seek(iobuf->_fd, -1, 1);
  171.      }
  172.  
  173.     iobuf -> _nleft = (NSECTS * SECSIZ);
  174.     iobuf -> _nextp = iobuf -> _buff;
  175.     return OK;
  176. }
  177.  
  178. int _fclose(iobuf)
  179. struct _buf *iobuf;
  180. {
  181.     /* comment out -----
  182.     if (iobuf < 4) return OK;
  183.     ----- end comment out */
  184.  
  185.     return BDS_close(iobuf -> _fd);
  186. }
  187.  
  188.  *iobuf -> _nextp++ , i) != i) {
  189.             return ERROR;
  190.     }
  191.  
  192.     i = (i-1) * SECSIZ;
  193.  
  194.     if (iobuf -> _nleft) {
  195.         movmem(iobuf->_buff + i, iobuf->_buff,